package com.alameda.service.manage;

import com.alameda.api.manage.TechnicalIndicatorService;
import com.alameda.api.statement.SystemRecordService;
import com.alameda.manage.bo.TechnicalIndicatorBO;
import com.alameda.manage.dto.TechnicalIndicatorDTO;
import com.alameda.manage.enums.StateEnum;
import com.alameda.manage.pojo.TechnicalIndicatorDO;
import com.alameda.manage.vo.TechnicalIndicatorVO;
import com.alameda.mapper.manage.TechnicalIndicatorMapper;
import com.alameda.statement.enums.SystemRecordEnum;
import com.alameda.utils.ExcelUtils;
import com.alameda.utils.MapUtils;
import com.alameda.utils.ObjectUtils;
import com.alameda.utils.PubUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 技术指标 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2024/08/04
 */
@Slf4j
@Service
public class TechnicalIndicatorServiceImpl extends ServiceImpl<TechnicalIndicatorMapper, TechnicalIndicatorDO> implements TechnicalIndicatorService {

    @Autowired
    private TechnicalIndicatorMapper technicalIndicatorMapper;

    /**
     * 查询技术指标列表
     *
     * @param technicalIndicatorDTO 技术指标数据传递对象
     * @return
     */
    @Override
    public IPage<TechnicalIndicatorVO> loadTechnicalIndicator(TechnicalIndicatorDTO technicalIndicatorDTO) {
        IPage<TechnicalIndicatorDO> page = new Page<>(technicalIndicatorDTO.getPageNum(), technicalIndicatorDTO.getPageSize());

        IPage<TechnicalIndicatorVO> technicalIndicatorPage = technicalIndicatorMapper.findTechnicalIndicatorInfo(page,
                technicalIndicatorDTO.getProjectId(),
                technicalIndicatorDTO.getTechnicalIndicatorRemark(),
                technicalIndicatorDTO.getState(),
                technicalIndicatorDTO.getPriority(),
                technicalIndicatorDTO.getIndexNo());

        technicalIndicatorPage.getRecords().forEach(item -> item.setStateRemark(StateEnum.getValueByKey(item.getState())));
        return technicalIndicatorPage;
    }

    @Autowired
    private SystemRecordService systemRecordService;

    /**
     * 新增或修改技术指标
     *
     * @param technicalIndicatorDTO 技术指标数据传递对象
     * @return
     */
    @Override
    public Boolean saveOrUpdateTechnicalIndicator(TechnicalIndicatorDTO technicalIndicatorDTO) {
        Map<String, String> extDataMap = Optional.ofNullable(technicalIndicatorDTO.getExtData()).orElse(MapUtils.newHashMap());
        String priority = technicalIndicatorDTO.getPriority();

        if (PubUtils.isNull(technicalIndicatorDTO.getId())) {
            // 新增场景
            extDataMap.put("top", priority);
            extDataMap.put("indexNo", getIndexNo());
        } else {
            // 修改场景
            extDataMap.put("top", priority);
        }

        TechnicalIndicatorDO technicalIndicatorDO = ObjectUtils.convert(technicalIndicatorDTO, TechnicalIndicatorDO.class);
        technicalIndicatorDO.setExtData(extDataMap);
        systemRecordService.saveRecord(SystemRecordEnum.DATA_COUNT);
        return this.saveOrUpdate(technicalIndicatorDO);
    }

    /**
     * 获取技术指标编号
     *
     * @return
     */
    private String getIndexNo() {
        Date date = new Date();
        String format = "yyyyMMddHHmmss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String formatDate = simpleDateFormat.format(date);
        return "Te-" + formatDate;
    }

    /**
     * 导出技术指标信息
     *
     * @param technicalIndicatorDTO 技术指标数据传递对象
     */
    @Override
    public void export(TechnicalIndicatorDTO technicalIndicatorDTO) {
        List<TechnicalIndicatorVO> technicalIndicatorVOList = getExportData(technicalIndicatorDTO);

        List<TechnicalIndicatorBO> technicalIndicatorBOList = technicalIndicatorVOList.stream().map(item -> {
            TechnicalIndicatorBO technicalIndicatorBO = ObjectUtils.convert(item, TechnicalIndicatorBO.class);
            technicalIndicatorBO.setProjectName(item.getProjectName());
            technicalIndicatorBO.setStateRemark(StateEnum.getValueByKey(item.getState()));
            technicalIndicatorBO.setPriority(Optional.ofNullable(item.getExtData()).orElse(MapUtils.newHashMap()).get("top"));
            technicalIndicatorBO.setIndexNo(Optional.ofNullable(item.getExtData()).orElse(MapUtils.newHashMap()).get("indexNo"));
            return technicalIndicatorBO;
        }).collect(Collectors.toList());

        ExcelUtils.export(technicalIndicatorBOList, TechnicalIndicatorBO.class, "技术指标信息");
    }

    /**
     * 通过项目id集合删除
     *
     * @param projectIdList id集合
     * @return
     */
    @Override
    public Boolean removeByProjectIdList(List<Long> projectIdList) {
        Wrapper<TechnicalIndicatorDO> wrapper = Wrappers.lambdaUpdate(TechnicalIndicatorDO.class)
                .in(TechnicalIndicatorDO::getProjectId, projectIdList);
        return this.remove(wrapper);
    }

    /**
     * 获取导出数据
     *
     * @param technicalIndicatorDTO 技术指标数据传递对象
     * @return
     */
    private List<TechnicalIndicatorVO> getExportData(TechnicalIndicatorDTO technicalIndicatorDTO) {
        List<Long> idList = technicalIndicatorDTO.getIdList();
        if (PubUtils.isEmpty(idList)) {
            IPage<TechnicalIndicatorVO> pageData = loadTechnicalIndicator(technicalIndicatorDTO);
            return pageData.getRecords();
        } else {
            return technicalIndicatorMapper.findByIdIn(idList);
        }
    }
}
